<template>
  <div class="change-password">
    <el-card>
      <template #header>
        <page-header title="修改密码" />
      </template>

      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="change-password-form"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input
            v-model="form.oldPassword"
            type="password"
            placeholder="请输入原密码"
            show-password
          />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="form.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            placeholder="请再次输入新密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">确认修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { adminApi } from '@/api/adminApi'
import PageHeader from '@/components/PageHeader.vue'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref(null)
const form = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (form.confirmPassword !== '') {
      formRef.value.validateField('confirmPassword')
    }
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== form.newPassword) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = {
  oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
  newPassword: [{ required: true, validator: validatePass, trigger: 'blur' }],
  confirmPassword: [{ required: true, validator: validatePass2, trigger: 'blur' }]
}

const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    const res = await adminApi.changePassword({
      currentPassword: form.oldPassword,
      newPassword: form.newPassword
    })
    
    if (res.code === 200) {
      ElMessage.success('密码修改成功，请重新登录')
      // 清空表单
      form.oldPassword = ''
      form.newPassword = ''
      form.confirmPassword = ''
      
      // 清除用户信息和token并跳转
      userStore.logout()
      router.push('/admin/login')
    }
  } catch (error) {
    console.error('Change password error:', error)
    ElMessage.error('修改密码失败：' + (error.response?.data?.message || '未知错误'))
  }
}
</script>

<style lang="scss" scoped>
.change-password {
  padding: 0;
  background-color: #fff;
  min-height: 100%;
  max-width: none !important;
  margin: 0 !important;
}

.el-card {
  border: none;
  box-shadow: none !important;
  margin: 0 !important;
}

.el-card :deep(.el-card__header) {
  padding: 0;
  border-bottom: 1px solid #e2e8f0;
  margin: 0;
}

.el-card :deep(.el-card__body) {
  padding: 0;
}

.change-password-form {
  padding: 20px;
}

:deep(.el-form) {
  max-width: 400px;
  margin: 20px 0 0 20px;
}

:deep(.el-form-item__label) {
  color: #475569;
  font-weight: 500;
}

:deep(.el-input__wrapper) {
  background-color: #f8fafc;
  box-shadow: 0 0 0 1px #e2e8f0;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #3b82f6;
}

:deep(.el-button) {
  height: 36px;
  padding: 0 16px;
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .change-password-form {
    padding: 16px;
  }

  :deep(.el-form) {
    max-width: 90%;
    margin: 12px 0 0 12px;
  }

  :deep(.el-form-item) {
    margin-bottom: 20px;
  }

  :deep(.el-form-item__label) {
    text-align: left !important;
    line-height: 40px;
    padding: 0;
    margin-left: -16px;
    font-size: 14px;
  }

  :deep(.el-form-item__content) {
    margin-left: 0 !important;
    display: flex;
    align-items: center;
    padding: 0 8px;
  }

  :deep(.el-input__wrapper) {
    height: 40px;
  }

  :deep(.el-button) {
    width: 100%;
    height: 40px;
    font-size: 14px;
    margin-left: 8px;
  }
}
</style>
